using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.IO;
using QuickLaunch.Properties;

namespace QuickLaunch
{
    public partial class DirSettingControl : UserControl
    {
        public DirSettingControl()
        {
            InitializeComponent();
        }

        private void DirSettingControl_Load(object sender, EventArgs e)
        {
            this.list_Directories.Items.AddRange(Program.qlDirSetting.GetSettingItems().ToArray());
            this._currentExcludeExt = Program.qlOtherSettings.GetValue(Resources.ExcludeExt);
            if (this._currentExcludeExt == null) this._currentExcludeExt = "";
            this.tb_ExludeExt.Text = this._currentExcludeExt;
        }

        private string _currentExcludeExt;

        #region Events

        public event SettingChangedEventHandler SettingChanged;

        private bool hasSettingChanged = false;

        #endregion

        private void btn_BrowseDir_Click(object sender, EventArgs e)
        {
            if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                // get the file name
                this.tb_AddDir.Text = this.folderBrowserDialog1.SelectedPath;
            }
        }

        private void btn_AddDir_Click(object sender, EventArgs e)
        {
            string item = this.tb_AddDir.Text.Trim();
            if (string.IsNullOrEmpty(item))
            {
                if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                    // get the file name
                    item = this.folderBrowserDialog1.SelectedPath;
                else
                    return;
            }

            // check if the item already exists in the list
            if (!this.list_Directories.Items.Contains(item))
            {
                // add the directory into the list
                this.list_Directories.Items.Add(item);
                this.hasSettingChanged = true;
                if (this.SettingChanged != null)
                    this.SettingChanged(this, new SettingChangedEventArgs("Directories"));
            }
            // clear the text box
            this.tb_AddDir.Text = "";
        }

        private void btn_RemoveDir_Click(object sender, EventArgs e)
        {
            if (this.list_Directories.SelectedIndex < 0)
                return;
            this.list_Directories.Items.RemoveAt(this.list_Directories.SelectedIndex);
            this.hasSettingChanged = true;
            if (this.SettingChanged != null)
                this.SettingChanged(this, new SettingChangedEventArgs("Directories"));
        }

        private void btn_RemoveAllDirs_Click(object sender, EventArgs e)
        {
            this.list_Directories.Items.Clear();
            this.hasSettingChanged = true;
            if (this.SettingChanged != null)
                this.SettingChanged(this, new SettingChangedEventArgs("Directories"));
        }

        public void SaveChanges()
        {
            if (this.hasSettingChanged)
            {
                // save the dir settings
                List<string> items = new List<string>(this.list_Directories.Items.Count);
                foreach (object item in this.list_Directories.Items)
                    items.Add((string)item);

                Program.qlDirSetting.Reset(items);

                // save the exclude ext setting
                if (Program.qlOtherSettings.GetValue(Resources.ExcludeExt) == null)
                    Program.qlOtherSettings.Add(Resources.ExcludeExt, this.tb_ExludeExt.Text);
                else
                    Program.qlOtherSettings.SetValue(Resources.ExcludeExt, this.tb_ExludeExt.Text);
            }
        }

        private void list_Directories_DoubleClick(object sender, EventArgs e)
        {
            this.btn_OpenDir_Click(null, null);
        }

        private void btn_OpenDir_Click(object sender, EventArgs e)
        {
            // open the selected item
            string dir = (string)this.list_Directories.SelectedItem;
            if (!string.IsNullOrEmpty(dir) && Directory.Exists(dir))
                Shell.Open(dir);
        }

        private void tb_ExludeExt_TextChanged(object sender, EventArgs e)
        {
            // compare the new value with old one, to see if any change is made.
            if (this._currentExcludeExt != this.tb_ExludeExt.Text)
            {
                this.hasSettingChanged = true;
                if (this.SettingChanged != null)
                    this.SettingChanged(this, new SettingChangedEventArgs("Directories"));
            }
        }
    }
}
